define(function(require, exports){
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var popwin = require('@base/dialog').base;

	// 添加条件里面的选择弹窗模块
	var CheckBoxPopwin = popwin.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'data': []
				,"class": 'M-addCondition'
				,"buttons":["clear", "cancel", "ok", "all", "opposite"]
				,'buttonConfig': {
					"clear": {
						"width": 65
						,"class":"btn btn-link clearButton"
						,"value":LANG("清空")
						,"attr":{"data-action":"onClear"}
					},
					"cancel": {
						"class": "btn btn-default"
					},
					"all": {
						"type":"button"
						,"width": 100
						,"layout":null
						,"value":LANG("全选")
						,"class": "btn btn-default fl"
						,"attr":{"data-action":"onAll"}
						,"events":"click"
					},
					"opposite": {
						"type":"button"
						,"width": 100
						,"layout":null
						,"value":LANG("反选")
						,"class": "btn btn-default fl"
						,"attr":{"data-action":"onOpposite"}
						,"events":"click"
					}
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self = this;
			self.Super('afterBuild', arguments);
			var c = self.getConfig();
			var single = self.getConfig('singleSelect');
			var el = self.getContainer('body').addClass(self.getConfig('class'));
			// 创建弹窗
			var popwin = $([
					'<div class="M-addCondition">',
						'<div class="checkBoxGroup" pub-name="checkBoxGroup" pub-mod="@base/common/input.checkbox"/>',
					'</div>'
			].join(''));

			if (single) {
				popwin.find('[data-type=all]').hide();
				popwin.find('[data-type=inverse]').hide();
			}

			popwin.appendTo(el);

			var config = {
				'checkBoxGroup': {
					'data': c.data,
					'key:': 'id',
					'name': 'name',
					'labelWidth': '18%',
					'labelHeight': 30
				}
			};

			self.buildFromTemplate(config);
		},
		onInputClick: function(ev){
			var cs = this.$, unsels, sels;
			var checkBoxGroup = cs.checkBoxGroup;
			var list = checkBoxGroup.getDOM();
			var name = ev.name;
			switch (name){
				case 'buttonClear':
					checkBoxGroup.reset();
					break;
				case 'buttonAll':
					unsels = list.find('input:not(:checked)');
					list.find(':checkbox').prop('checked', Boolean(unsels.length));
					break;
				case 'buttonOpposite':
					sels = list.find('input:checked');
					unsels = list.find('input:not(:checked)');
					sels.prop('checked', false);
					unsels.prop('checked', true);
					break;
				case 'buttonCancel':
					this.hide();
					return false;
					break;
				case 'buttonOk':
					var data = checkBoxGroup.getData('data');
					if(!data || !data.length)
					{
						pubjs.alert(LANG('请至少选择一个选项'));
						return false;
					}
					this.fire('saveLabel', data);
					this.reset();
					return false;
					break;
			}
		},
		setData: function(data){
			this.$.checkBoxGroup.setData(data, 'data-id');
			return this;
		},
		buildList: function(data)
		{
			this.$.checkBoxGroup.buildList(data);
			return this;
		},
		reset: function(){
			var cs = this.$;
			cs.checkBoxGroup.reset();
			this.hide();
			return this;
		}
	});

	exports.base = CheckBoxPopwin;
});